package yxy.game.pm2.active.command.ccjj;

import yxy.cherry.data.bean.actgradefund;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_成长基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActCzjjDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

@MessageType(proto = Command.ActCzjjDrawReward, classtype = ActiveMessage.ActCzjjDrawRewardRequest.class)
public class CMD_Active_成长基金_DrawReward extends Command<ActCzjjDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActCzjjDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActCzjjDrawRewardResponse.Builder response = ActiveMessage.ActCzjjDrawRewardResponse.newBuilder();

		int id = request.getId();

		ActiveModule_成长基金 am = ActiveModule_成长基金.get(pcode);
		if (am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		actgradefund agf = ActDataCenter.getData(id, actgradefund.class, pcode);
		if (agf == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		ArrayList<Integer> getids = am.getGetids();
		if (getids.contains(id)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}

		ArrayList<Currency> curs = new ArrayList<Currency>();
		curs.add(Currency.create(agf.getRewardtype(), agf.getRewardid(), agf.getRewardnum()));
		curs.add(Currency.create(agf.getRewardtype2(), agf.getRewardid2(), agf.getRewardnum2()));

		am.adddrawid(id);
		new CurrencyHandle().addCurrency(pcode, CCReason.活动_成长基金, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActCzjjDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
